package com.tudouji.project.product.controller;

import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.product.domain.Brand;
import com.tudouji.project.product.domain.Category;
import com.tudouji.project.product.domain.Productinfo;
import com.tudouji.project.product.domain.Salesspecification;
import com.tudouji.project.product.service.ISalesspecificationService;
import com.tudouji.project.product.service.impl.BrandServiceImpl;
import com.tudouji.project.product.service.impl.CategoryServiceImpl;
import com.tudouji.project.product.service.impl.ProductinfoServiceImpl;
import com.tudouji.project.system.domain.SysDictData;
import com.tudouji.project.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品规格Controller
 *
 * @author wf
 * @date 2020-12-30
 */
@RestController
@RequestMapping("/product/salesspecification")
public class SalesspecificationController extends BaseController
{
    @Autowired
    private ISalesspecificationService salesspecificationService;
    @Autowired
    private BrandServiceImpl brandService;
    @Autowired
    private ProductinfoServiceImpl productinfoService;
    @Autowired
    private CategoryServiceImpl categoryService;
    @Autowired
    private SysDictDataMapper dictDataService;

    /**
     * 查询产品规格列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Salesspecification salesspecification)
    {
        startPage();
        List<Salesspecification> list = salesspecificationService.selectSalesspecificationListPC(salesspecification);
        return getDataTable(list);
    }

    /**
     * 导出产品规格列表
     */
    @PreAuthorize("@ss.hasPermi('product:salesspecification:export')")
    @Log(title = "产品规格", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Salesspecification salesspecification)
    {
        List<Salesspecification> list = salesspecificationService.selectSalesspecificationListPC(salesspecification);
        ExcelUtil<Salesspecification> util = new ExcelUtil<Salesspecification>(Salesspecification.class);
        return util.exportExcel(list, "salesspecification");
    }

    /**
     * 获取产品规格详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:salesspecification:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        Salesspecification salesspecification = salesspecificationService.selectSalesspecificationById(guid);
        salesspecification.setDataNum(this.salesspecificationService.checkSpguid(guid));
        return AjaxResult.success(salesspecification);
    }

    /**
     * 新增产品规格
     */
    @PreAuthorize("@ss.hasPermi('product:salesspecification:add')")
    @Log(title = "产品规格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Salesspecification salesspecification)
    {
        Salesspecification sa = new Salesspecification();
        sa.setProductguid(salesspecification.getProductguid());
        sa.setName1(salesspecification.getName());
        List<Salesspecification> nameList = this.salesspecificationService.selectSalesspecificationList(sa);
        if(nameList.size() > 0){
            return AjaxResult.error("该品种下已经存在该规格，不允许重复添加");
        }
        if(salesspecification.getBaseFlag() == 1){
            if (salesspecificationService.checkBaseFlag(null,salesspecification.getProductguid(), salesspecification.getType())){
                return AjaxResult.error("该品种下已经设置基础原料，不允许重复设置");
            }
        }
        return toAjax(salesspecificationService.insertSalesspecification(salesspecification));
    }

    /**
     * 修改产品规格
     */
    @PreAuthorize("@ss.hasPermi('product:salesspecification:edit')")
    @Log(title = "产品规格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Salesspecification salesspecification)
    {
        Salesspecification sa = new Salesspecification();
        sa.setGuid(salesspecification.getGuid());
        sa.setProductguid(salesspecification.getProductguid());
        sa.setName1(salesspecification.getName());
        List<Salesspecification> nameList = this.salesspecificationService.selectSalesspecificationList(sa);
        if(nameList.size() > 0){
            return AjaxResult.error("该品种下已经存在该规格，不允许重复添加");
        }
        if(salesspecification.getBaseFlag() != null){
            if(salesspecification.getBaseFlag() == 1){
                if (salesspecificationService.checkBaseFlag(salesspecification.getGuid(),salesspecification.getProductguid(), salesspecification.getType())){
                    return AjaxResult.error("该品种下已经设置基础原料，不允许重复设置");
                }
            }
        }

        return toAjax(salesspecificationService.updateSalesspecification(salesspecification));
    }

    /**
     * 删除产品规格
     */
    @PreAuthorize("@ss.hasPermi('product:salesspecification:remove')")
    @Log(title = "产品规格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        boolean flag = false;
        for(String guid : guids){
          flag =  this.salesspecificationService.checkSpguid(guid);
          if(flag){
              break;
          }
        }
        if(flag){
            return AjaxResult.error("选中的数据中有已被使用，不允许删除");
        }
        return toAjax(salesspecificationService.deleteSalesspecificationByIds(guids));
    }

    /**
     * 查询产品规格列表
     */
    @GetMapping("/findSpecificationList")
    public AjaxResult findSpecificationList(Salesspecification salesspecification)
    {
        List<Salesspecification> list = salesspecificationService.selectSalesspecificationList(salesspecification);
        return AjaxResult.success(list);
    }

    /**
     * 获取套装添加基础数据
     */
    @GetMapping("/getBaseInfo")
    public AjaxResult getBaseInfo(Salesspecification salesspecification)
    {
        AjaxResult ajaxResult = AjaxResult.success();
        List<Brand> brands = brandService.selectBrandList(new Brand());
        List<Productinfo> products = productinfoService.selectProductinfoList(new Productinfo());
        List<Category> categorys = categoryService.selectCategoryList(new Category());
        List<SysDictData> dicts = dictDataService.selectDictDataByType("product_type");
        List<Salesspecification> list = salesspecificationService.selectSalesspecificationList(salesspecification);
        ajaxResult.put("brands", brands);
        ajaxResult.put("products", products);
        ajaxResult.put("categorys", categorys);
        ajaxResult.put("dicts", dicts);
        ajaxResult.put("list", list);
        return ajaxResult;
    }
}
